{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-19-72204f36b2aa>:16: DeprecationWarning: use options instead of chrome_options\n",
      "  driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import time\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.desired_capabilities import DesiredCapabilities\n",
    "import requests\n",
    "import json\n",
    "import base64\n",
    "import hashlib\n",
    "\n",
    "opts = webdriver.ChromeOptions()\n",
    "opts.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错\n",
    "opts.add_argument('window-size=1920x3000') #指定浏览器分辨率\n",
    "opts.add_argument('--disable-gpu') #谷歌文档提到需要加上一这个属性来规避bug\n",
    "opts.add_argument('--hide-scrollbars') #隐藏滚动条, 应对些特殊页面\n",
    "\n",
    "driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 打开CNKI\n",
    "* 校园网，自动登录 cnki.net\n",
    "* 校外网，需要登陆fsso.cnki.net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get('https://cnki.net')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 检查是否用中山大学南方学院登录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中山大学'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.find_element_by_id('Ecp_loginShowName1').get_attribute('innerHTML')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 打开高级检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_id('highSearch')\n",
    "element.get_attribute('innerHTML')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'CDwindow-3B78E08926E7F6C509C5186CA45BFEC4'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.current_window_handle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['CDwindow-3B78E08926E7F6C509C5186CA45BFEC4',\n",
       " 'CDwindow-DC36C5C263E3A6AFCC5D379E1C2B3FE2']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.window_handles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-26-6c6d5ce6602d>:1: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[1])\n"
     ]
    }
   ],
   "source": [
    "driver.switch_to_window(driver.window_handles[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 点击学术期刊"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//ul[@class=\"doctype-menus keji\"]/li[@data-id=\"xsqk\"]/a').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 点击专业检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_name('majorSearch').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 选择刊期来源"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//div[@class=\"extend-tit-labels\"]//input[@key=\"SI\"]').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//div[@class=\"extend-tit-labels\"]//input[@key=\"EI\"]').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//div[@class=\"extend-tit-labels\"]//input[@key=\"HX\"]').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//div[@class=\"extend-tit-labels\"]//input[@key=\"CSI\"]').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设置搜索query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = 'SU=\"教育\" AND KY=\"科学\"'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_xpath('//textarea')\n",
    "element.clear()\n",
    "element.send_keys(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//input[@value=\"检索\"]').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 检车检索信息 ，更换页面文章数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "element = driver.find_element_by_xpath('//*[@id=\"perPageDiv\"]/div/span')\n",
    "element.get_attribute('innerHTML')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//i[@class=\"icon icon-sort\"]').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//div[@id=\"perPageDiv\"]//li[@data-val=\"10\"]/a').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 抓取页面信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>篇名</th>\n",
       "      <th>作者</th>\n",
       "      <th>刊名</th>\n",
       "      <th>发表时间</th>\n",
       "      <th>被引</th>\n",
       "      <th>下载</th>\n",
       "      <th>操作</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>运用科学培养集体意识:涂尔干的思考及其启示</td>\n",
       "      <td>孔祥渊</td>\n",
       "      <td>上海教育科研</td>\n",
       "      <td>2021-06-15</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>科学人生观：王星拱及其科学思想述评</td>\n",
       "      <td>黄瑞雄; 俞光华</td>\n",
       "      <td>自然辩证法通讯</td>\n",
       "      <td>2021-05-27</td>\n",
       "      <td>NaN</td>\n",
       "      <td>110.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>教科书上的“非典”——科学课程中的防疫教育</td>\n",
       "      <td>肖龙海; 叶德伟</td>\n",
       "      <td>上海教育科研</td>\n",
       "      <td>2021-05-15</td>\n",
       "      <td>NaN</td>\n",
       "      <td>109.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>魔法与科学:人工智能的教育迷思及其祛魅</td>\n",
       "      <td>杨欣</td>\n",
       "      <td>教育学报</td>\n",
       "      <td>2021-04-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>85.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>创新型科技人才培养新思路——基于艺术和科学融合的视角</td>\n",
       "      <td>朱芬; 孔燕</td>\n",
       "      <td>科技管理研究</td>\n",
       "      <td>2021-03-10</td>\n",
       "      <td>NaN</td>\n",
       "      <td>94.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>教育研究中的“实证”与“经验”之辨——基于方法论的反思</td>\n",
       "      <td>孙嘉蔚</td>\n",
       "      <td>苏州大学学报(教育科学版)</td>\n",
       "      <td>2020-11-19</td>\n",
       "      <td>1.0</td>\n",
       "      <td>370.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>教育学“科学还是技艺”的历史重审——从夸美纽斯出发的思想史研究</td>\n",
       "      <td>娄雨</td>\n",
       "      <td>教育研究</td>\n",
       "      <td>2020-07-15</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1096.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>论杜威的技术探究对教育研究的启示</td>\n",
       "      <td>覃泽宇</td>\n",
       "      <td>中国电化教育</td>\n",
       "      <td>2020-07-10</td>\n",
       "      <td>NaN</td>\n",
       "      <td>406.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>小学是一门科学</td>\n",
       "      <td>谢维和</td>\n",
       "      <td>人民教育</td>\n",
       "      <td>2020-06-18</td>\n",
       "      <td>NaN</td>\n",
       "      <td>221.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>康德的实践教育学:在“理想”与“科学”之间</td>\n",
       "      <td>李长伟</td>\n",
       "      <td>湖南师范大学教育科学学报</td>\n",
       "      <td>2020-04-22 09:35</td>\n",
       "      <td>NaN</td>\n",
       "      <td>389.0</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0                               篇名        作者             刊名  \\\n",
       "0           1            运用科学培养集体意识:涂尔干的思考及其启示       孔祥渊         上海教育科研   \n",
       "1           2                科学人生观：王星拱及其科学思想述评  黄瑞雄; 俞光华        自然辩证法通讯   \n",
       "2           3            教科书上的“非典”——科学课程中的防疫教育  肖龙海; 叶德伟         上海教育科研   \n",
       "3           4              魔法与科学:人工智能的教育迷思及其祛魅        杨欣           教育学报   \n",
       "4           5       创新型科技人才培养新思路——基于艺术和科学融合的视角    朱芬; 孔燕         科技管理研究   \n",
       "5           6      教育研究中的“实证”与“经验”之辨——基于方法论的反思       孙嘉蔚  苏州大学学报(教育科学版)   \n",
       "6           7  教育学“科学还是技艺”的历史重审——从夸美纽斯出发的思想史研究        娄雨           教育研究   \n",
       "7           8                 论杜威的技术探究对教育研究的启示       覃泽宇         中国电化教育   \n",
       "8           9                          小学是一门科学       谢维和           人民教育   \n",
       "9          10            康德的实践教育学:在“理想”与“科学”之间       李长伟   湖南师范大学教育科学学报   \n",
       "\n",
       "               发表时间   被引      下载  操作  \n",
       "0        2021-06-15  NaN     NaN  下载  \n",
       "1        2021-05-27  NaN   110.0  下载  \n",
       "2        2021-05-15  NaN   109.0  下载  \n",
       "3        2021-04-25  NaN    85.0  下载  \n",
       "4        2021-03-10  NaN    94.0  下载  \n",
       "5        2020-11-19  1.0   370.0  下载  \n",
       "6        2020-07-15  1.0  1096.0  下载  \n",
       "7        2020-07-10  NaN   406.0  下载  \n",
       "8        2020-06-18  NaN   221.0  下载  \n",
       "9  2020-04-22 09:35  NaN   389.0  下载  "
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "element = driver.find_element_by_id('gridTable')\n",
    "表格 = element.get_attribute('innerHTML')\n",
    "pd.read_html(表格)[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 翻页和下载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 下载和翻页函数\n",
    "# def download():\n",
    "#     for pages in range(0,100):\n",
    "#         click_list = driver.find_elements_by_xpath('//table[@class=\"result-table-list\"]//a[@class=\"fz14\"]')\n",
    "\n",
    "#         for i in click_list:\n",
    "#             i.click()\n",
    "#             driver.switch_to_window(driver.window_handles[2])\n",
    "#             element = driver.find_element_by_xpath('//*[@id=\"pdfDown\"]')\n",
    "#             driver.execute_script(\"arguments[0].click();\",element)\n",
    "#             time.sleep(10)\n",
    "#             driver.close()\n",
    "#             driver.switch_to_window(driver.window_handles[1])\n",
    "\n",
    "#         driver.find_element_by_id('PageNext').click()\n",
    "#         time.sleep(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 下载和翻页函数\n",
    "# def download(i):\n",
    "#     i.click()\n",
    "#     driver.switch_to_window(driver.window_handles[2])\n",
    "#     element = driver.find_element_by_xpath('//*[@id=\"pdfDown\"]')\n",
    "#     driver.execute_script(\"arguments[0].click();\",element)\n",
    "#     time.sleep(10)\n",
    "#     driver.close()\n",
    "#     driver.switch_to_window(driver.window_handles[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下载路径\n",
    "path = r\"C:\\Users\\86185\\Downloads\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "def VC():    \n",
    "    element = driver.find_element_by_xpath('//*[@id=\"vImg\"]')\n",
    "    element.screenshot(\"E:\\学习\\作业\\Python数据挖掘\\期末项目_CNKI\\data-mining-final-project-cnki\\Verification_Code.png\")\n",
    "\n",
    "    with open(r\"E:\\学习\\作业\\Python数据挖掘\\期末项目_CNKI\\data-mining-final-project-cnki\\Verification_Code.png\", 'rb') as f:\n",
    "            Verification_Code = f.read()\n",
    "\n",
    "    from header import header    \n",
    "    url = \"https://webapi.xfyun.cn/v1/service/v1/ocr/general\"    \n",
    "    header = header()\n",
    "    data_base64 = str(base64.b64encode(Verification_Code), 'utf-8')\n",
    "    data = {'image': data_base64}\n",
    "    response = requests.post(url, data, headers=header)\n",
    "    VC = response.json()['data']['block'][0]['line'][0]['word'][0]['content']\n",
    "    \n",
    "    return VC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-85-273453a77fbd>:1: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[1])\n"
     ]
    }
   ],
   "source": [
    "driver.switch_to_window(driver.window_handles[1]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本有14篇\n",
      "打开页面\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-86-a14a90a89308>:17: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[2])\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下载本篇文章\n",
      "have vc\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-86-a14a90a89308>:25: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[3])\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入BBDN\n",
      "现有15篇\n",
      "下载成功\n",
      "关闭当前窗口\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-86-a14a90a89308>:71: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[2])\n",
      "<ipython-input-86-a14a90a89308>:75: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[2])\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有15篇\n",
      "打开页面\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-86-a14a90a89308>:80: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[1])\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入LDKS\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入BT9N\n",
      "现有16篇\n",
      "下载成功\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有16篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入IMBE\n",
      "获取验证码\n",
      "获取验证码失败，重新获取验证码\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入59EE\n",
      "现有17篇\n",
      "下载成功\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有17篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "获取验证码失败，重新获取验证码\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入CX3S\n",
      "现有18篇\n",
      "下载成功\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有18篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入244D\n",
      "现有19篇\n",
      "下载成功\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有19篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入N6YJ\n",
      "现有20篇\n",
      "下载成功\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有20篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "3\n",
      "重新获取验证码\n",
      "获取验证码\n",
      "3\n",
      "重新获取验证码\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入432S\n",
      "现有21篇\n",
      "下载成功\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有21篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入BDBC\n",
      "现有22篇\n",
      "下载成功\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有22篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "1\n",
      "重新获取验证码\n",
      "获取验证码\n",
      "1\n",
      "重新获取验证码\n",
      "获取验证码\n",
      "获取验证码失败，重新获取验证码\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入XM3A\n",
      "现有23篇\n",
      "下载成功\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有23篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入82SD\n",
      "获取验证码\n",
      "6\n",
      "重新获取验证码\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入YYIP\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入5NDA\n",
      "现有23篇\n",
      "下载失败\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "翻到下一页\n",
      "本有23篇\n",
      "打开页面\n",
      "下载本篇文章\n",
      "have vc\n",
      "获取验证码\n",
      "4\n",
      "yes\n",
      "输入36BL\n",
      "现有23篇\n",
      "下载失败\n",
      "关闭当前窗口\n",
      "关闭当前窗口\n",
      "no vc\n",
      "回到下载列表\n",
      "------------\n",
      "本有23篇\n",
      "打开页面\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-86-a14a90a89308>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     16\u001b[0m         \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     17\u001b[0m         \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mswitch_to_window\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwindow_handles\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m         \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     19\u001b[0m         \u001b[0melement\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element_by_xpath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'//*[@id=\"pdfDown\"]'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     20\u001b[0m         \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute_script\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"arguments[0].click();\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0melement\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "for pages in range(1,10):\n",
    "\n",
    "    # 翻页\n",
    "    click_list = driver.find_elements_by_xpath('//table[@class=\"result-table-list\"]//a[@class=\"fz14\"]')\n",
    "    # 获取xpath列表\n",
    "        \n",
    "    for i in click_list:\n",
    "        total_num = os.listdir(path)\n",
    "        # 读取文件数量\n",
    "        num = len(total_num)\n",
    "        print('本有' + str(nums-1) + '篇')\n",
    "        # 循环1\n",
    "        i.click()\n",
    "        print('打开页面')\n",
    "        # 点击列表中xpath\n",
    "        time.sleep(2)\n",
    "        driver.switch_to_window(driver.window_handles[2])\n",
    "        time.sleep(2)\n",
    "        element = driver.find_element_by_xpath('//*[@id=\"pdfDown\"]')\n",
    "        driver.execute_script(\"arguments[0].click();\",element)\n",
    "        print('下载本篇文章')\n",
    "        # 点击pdf下载按钮\n",
    "        \n",
    "        try:\n",
    "            driver.switch_to_window(driver.window_handles[3])\n",
    "            print('have vc')\n",
    "            # 是否有提交验证码按钮可点\n",
    "            for x in range(100):\n",
    "                driver.find_element_by_xpath('//*[@id=\"vImg\"]').click()\n",
    "                print('获取验证码')\n",
    "                time.sleep(1)\n",
    "                try:                \n",
    "                    vc = VC()\n",
    "                    length = len(vc)\n",
    "                    print(length)\n",
    "                    time.sleep(1)\n",
    "                    # 查询验证码长度\n",
    "                    \n",
    "                    if length == 4:\n",
    "                        print('yes')\n",
    "                        # 打印验证码\n",
    "                        driver.find_element_by_xpath('//*[@id=\"vcode\"]').clear()\n",
    "                        time.sleep(1)\n",
    "                        driver.find_element_by_xpath('//*[@id=\"vcode\"]').send_keys(vc)\n",
    "                        print(\"输入\" + vc)\n",
    "                        time.sleep(1)\n",
    "                        driver.find_element_by_xpath('//dl[@class=\"c_verify-code\"]//button').click()\n",
    "                        time.sleep(2)\n",
    "                        # 点击输入框并输入\n",
    "\n",
    "                        try:\n",
    "                            # 判断是否验证成功\n",
    "                            driver.find_element_by_xpath('//dl[@class=\"c_verify-code\"]//button').click()\n",
    "                            time.sleep(2)\n",
    "                            driver.find_element_by_xpath('//dl[@class=\"c_verify-code\"]//button').click()\n",
    "                            driver.find_element_by_xpath('//*[@id=\"vImg\"]').click()                        \n",
    "                            continue\n",
    "\n",
    "                        except:\n",
    "\n",
    "                            total_num = os.listdir(path)\n",
    "                            # 读取文件数量\n",
    "                            nums = len(total_num)\n",
    "                            print('现有' + str(nums-1) + '篇')\n",
    "\n",
    "                            if nums != num:\n",
    "                                print('下载成功')\n",
    "                            else:\n",
    "                                print('下载失败')\n",
    "\n",
    "                            driver.switch_to_window(driver.window_handles[2])\n",
    "                            print('关闭当前窗口')\n",
    "                            driver.close()\n",
    "                            time.sleep(2)\n",
    "                            driver.switch_to_window(driver.window_handles[2])\n",
    "                            print('关闭当前窗口')\n",
    "                            driver.close()\n",
    "                            time.sleep(2)                        \n",
    "                            print('no vc')\n",
    "                            driver.switch_to_window(driver.window_handles[1])\n",
    "                            print('回到下载列表')\n",
    "                            print('------------')\n",
    "                            break                        \n",
    "\n",
    "                    else:                    \n",
    "                        print('重新获取验证码')\n",
    "                        time.sleep(1)\n",
    "                        continue\n",
    "                        \n",
    "                except:                                       \n",
    "                    print('获取验证码失败，重新获取验证码')\n",
    "                    continue\n",
    "                        \n",
    "        except:\n",
    "            print('no vc')\n",
    "            print(\"回到下载列表\")\n",
    "            time.sleep(5)\n",
    "            driver.switch_to_window(driver.window_handles[2])\n",
    "            print('返回下载窗口')\n",
    "            driver.close()\n",
    "            print('关闭当前窗口')\n",
    "            print('下载成功')\n",
    "            print('------------')\n",
    "            driver.switch_to_window(driver.window_handles[1])\n",
    "            driver.switch_to_window(driver.window_handles[1])\n",
    "            continue\n",
    "            \n",
    "    driver.find_element_by_id('PageNext').click()\n",
    "    print('翻到下一页')\n",
    "    time.sleep(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
